<?php

namespace App\Http\Controllers;

use App\Http\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
{

	public function index ( $id )
	{
		/*
		 * 测试laravel的orm功能，当不写with条件时，foreach会执行很多条sql语句
		 * \DB::connection()->enableQueryLog();

		$user = User::with(['posts'])->withCount(['posts','hasFans'])->get();

		foreach($user as $v){
			$posts[] = $v->posts;
		}

		dd($posts,\DB::connection()->getQueryLog());*/

		$user = User::with(['posts'])->withCount(['posts','hasFans'])->find($id);

		dd($user);
		return view('user.setting',compact('user'));

	}


	/**
	 * 登录动作
	 */
	public function login ( Request $request )
	{
		$this->validate($request ,[
				'email' => 'required',
				'password' => 'required',
		]);

		$isRemember = boolval(request('is_remember'));

//		$param = array_merge(request(['email']))

		if(\Auth::attempt(request(['email','password']),$isRemember)){
			return redirect('/post/index');
		}

		return redirect()->back()->withErrors('用户名密码错误');

	}

	public function logout (  )
	{
		\Auth::logout();
		return redirect('login/index');
	}
}
